<?php

namespace app\webapi\controller;

use app\webapi\common\BaseController;
use app\webapi\vms\service\LinkService;
use app\webapi\vms\validate\LinkValidate;
use library\OWException;
use think\App;

/**
 * @链接控制器
 */
class LinkController extends BaseController
{
    /**
     * @var LinkValidate
     */
    protected $validate;

    /**
     * @var LinkService
     */
    protected $service;

    /**
     * @param App $app
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->validate = new LinkValidate();
        $this->service = new LinkService();
    }

    /**
     * @desc 获取链接分页列表
     * @return \think\response\Json
     * @author zhaozhiwei
     * @time 2023/12/14-11:52
     */
    public function getPaginateList()
    {
        $params = $this->validate->goCheck('getPaginateList');
        $res = $this->service->getPaginateList($params['page_no'], $params['page_size'], $this->website_id, $params['type']);
        return $this->responseSuccess($res);
    }

    /**
     * @desc 编辑链接
     * @return \think\response\Json
     * @author zhaozhiwei
     * @time 2023/12/14-11:53
     */
    public function edit()
    {
        $params = $this->validate->goCheck('edit');
        $res = $this->service->edit($params, $this->website_id);
        return $this->responseSuccess($res);
    }

    /**
     * @desc 获取链接详情信息
     * @return \think\response\Json
     * @author zhaozhiwei
     * @time 2023/12/14-11:53
     */
    public function getInfo()
    {
        $params = $this->validate->goCheck('getInfo');
        $res = $this->service->getInfo($params['id'], $this->website_id);
        return $this->responseSuccess($res);
    }

    /**
     * @desc 删除链接
     * @return \think\response\Json
     * @author zhaozhiwei
     * @time 2023/12/14-11:53
     */
    public function delete()
    {
        $params = $this->validate->goCheck('delete');
        $id_arr = explode(',', $params['ids']);
        foreach ($id_arr as $v) {
            if (!is_numeric($v)) {
                throw (new OWException('参数格式错误'));
            }
        }
        $res = $this->service->delete($id_arr, $this->website_id);
        return $this->responseSuccess($res);
    }

}